package report.msg;

import entity.enums.HikMsgType;
import global.Global;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhouwz
 * @date 2020/11/3 15:09
 */
public class HeadMsg {

    static AtomicInteger seqCount;

    static {
        seqCount = new AtomicInteger();
    }

    public ByteBuffer headToByte(HikMsgType hikMsgType) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Global.HEAD_SIZE);
        byteBuffer
                .put("GBP$".getBytes())
                .putShort(hikMsgType.getLen())
                .putShort(hikMsgType.getCode())
                .putInt(seqCount.incrementAndGet())
                .put((byte) 0x20)
                .put((byte) 0)
                .put((byte) 0)
                .put(new byte[17])
                .flip();
        return byteBuffer;
    }
}
